/**
 * Copyright 2015 iMeth工作室 (http://www.imeth.cn). All rights reserved.
 * Encoding UTF-8
 * Version: 1.0
 * Date: 2015年3月30日
 * History:	
 */

package cn.imeth.app;

import org.apache.log4j.Appender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import android.app.Application;
import android.os.Environment;
import cn.imeth.log4j.Log;

/**
 * @author 戴文龙 (daiwenlong@imeth.cn)
 * @version Revision: 1.0.0 Date: 2015年3月30日
 */
public class ImethApplication extends Application {

    private String cacheFolder;

    @Override
    public void onCreate() {
        super.onCreate();

        initLog();
        initFileFolder();

    }

    /**
     * 初始化日志系统
     */
    protected void initLog() {
        Log.init(getLogFilePath());

        Log.init("iMeth", getLogFilePath(), getLogLevel(), isExternalStorageEmulated());
    }

    /**
     * 日志显示的级别
     */
    protected Level getLogLevel() {
        return Level.DEBUG;
    }

    /**
     * 初始化项目中所需要的文件
     */
    protected void initFileFolder() {
        initCacheFolder();
    }

    protected void initCacheFolder() {
        if (isExternalStorageEmulated()) {
            cacheFolder = this.getExternalCacheDir().getAbsolutePath();
        } else {
            cacheFolder = this.getCacheDir().getAbsolutePath();
        }
    }

    /**
     * 得到缓存文件目录
     */
    public String getCacheFolder() {
        return cacheFolder;
    }

    protected void addLogAppender(Appender appender) {
        Logger root = Logger.getRootLogger();
        root.addAppender(appender);
    }

    protected String getLogFilePath() {
        return getExternalFilesDir("logs").getAbsolutePath() + "/log.log";
    }

    /**
     * 是否存在外存
     */
    public boolean isExternalStorageEmulated() {
        return Environment.isExternalStorageEmulated();
    }

}
